#!/usr/bin/env python
"""
创建初始数据脚本
"""
import os
import sys
import django

# 设置Django环境
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'zhengzhou_mobile.settings')
django.setup()

from mobile_app.models import Plan, User, PhoneNumber, Balance, PhonePlan
from django.contrib.auth.hashers import make_password

def create_plans():
    """创建套餐数据"""
    plans_data = [
        {
            'name': '动感地带-青春版',
            'description': '适合学生用户，流量充足，语音适中',
            'monthly_fee': 39.00,
            'data_allowance': 2048,  # 2GB
            'voice_allowance': 100,
            'sms_allowance': 50,
        },
        {
            'name': '动感地带-畅享版',
            'description': '流量语音均衡，适合日常使用',
            'monthly_fee': 59.00,
            'data_allowance': 5120,  # 5GB
            'voice_allowance': 200,
            'sms_allowance': 100,
        },
        {
            'name': '动感地带-尊享版',
            'description': '高端套餐，流量语音充足',
            'monthly_fee': 99.00,
            'data_allowance': 10240,  # 10GB
            'voice_allowance': 500,
            'sms_allowance': 200,
        },
        {
            'name': '全球通-标准版',
            'description': '商务套餐，全国通话无漫游',
            'monthly_fee': 128.00,
            'data_allowance': 15360,  # 15GB
            'voice_allowance': 1000,
            'sms_allowance': 300,
        },
        {
            'name': '全球通-尊享版',
            'description': '顶级套餐，无限流量',
            'monthly_fee': 199.00,
            'data_allowance': 30720,  # 30GB
            'voice_allowance': 2000,
            'sms_allowance': 500,
        },
        {
            'name': '神州行-畅听版',
            'description': '经济实惠，语音为主',
            'monthly_fee': 29.00,
            'data_allowance': 1024,  # 1GB
            'voice_allowance': 300,
            'sms_allowance': 30,
        }
    ]
    
    for plan_data in plans_data:
        plan, created = Plan.objects.get_or_create(
            name=plan_data['name'],
            defaults=plan_data
        )
        if created:
            print(f"创建套餐: {plan.name}")
        else:
            print(f"套餐已存在: {plan.name}")

def create_test_user():
    """创建测试用户"""
    # 创建测试用户
    test_user, created = User.objects.get_or_create(
        username='13800138000',
        defaults={
            'email': 'test@example.com',
            'password': make_password('test123456'),
            'first_name': '测试',
            'last_name': '用户'
        }
    )
    
    if created:
        print(f"创建测试用户: {test_user.username}")
        
        # 创建用户资料
        from mobile_app.models import UserProfile
        UserProfile.objects.create(user=test_user)
        
        # 创建测试手机号码
        test_phone, created = PhoneNumber.objects.get_or_create(
            number='13800138000',
            defaults={
                'user': test_user,
                'is_primary': True
            }
        )
        
        if created:
            print(f"创建测试号码: {test_phone.number}")
            
            # 创建余额记录
            Balance.objects.create(
                phone_number=test_phone,
                amount=100.00,  # 赠送100元话费
                data_remaining=0,
                voice_remaining=0,
                sms_remaining=0
            )
            
            # 选择第一个套餐
            first_plan = Plan.objects.first()
            if first_plan:
                PhonePlan.objects.create(
                    phone_number=test_phone,
                    plan=first_plan
                )
                print(f"为测试号码设置套餐: {first_plan.name}")
                
                # 更新余额信息
                balance = test_phone.balance
                balance.update_allowance()
                balance.amount = 100.00  # 保持赠送的话费
                balance.save()
    else:
        print(f"测试用户已存在: {test_user.username}")

def main():
    print("开始创建初始数据...")
    
    # 创建套餐
    create_plans()
    
    # 创建测试用户
    create_test_user()
    
    print("初始数据创建完成！")
    print("\n测试账户信息:")
    print("用户名: 13800138000")
    print("密码: test123456")
    print("初始话费: ¥100.00")

if __name__ == '__main__':
    main()
